package com.kwm.Controller;


import com.kwm.Utils.AliyunOSSUtils;
import com.kwm.entity.AliyunOSSProperties;
import com.kwm.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@Slf4j
@RestController
public class UplodControll {

        //第一种方法
//    private final String bucketName = "javaweb-ttlias";
//    private final String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        //第二种方法
//    参数化配置后
//      @Value("${aliyun.oss.bucket}")
//      private String bucket;
//      @Value("${aliyun.oss.endpoint}")
//      private String endpoint;
    // 第三总方法
    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;
    @PostMapping("/upload")
    public Result uplod(MultipartFile file) throws Exception {

        //1.获取原始文件名，获取后缀。
        String originalFilename = file.getOriginalFilename();
        log.info("原始文件名为：{}", originalFilename);
        String expname = originalFilename.substring(originalFilename.lastIndexOf("."));
        //2.调用阿里云OSS工具类，将文件上传到oss
        String url = AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getBucket(), file.getBytes(), expname);
        return Result.success(url);
    }
}
